package jwdt.xadmin.bbs.service.impl;

import jwdt.common.dao.BaseDao;
import jwdt.xadmin.bbs.bean.TBuser;
import jwdt.xadmin.bbs.service.UserManageService;

import com.ezerg.db.Jdb;
import com.ezerg.db.ext.PageUtil;
import com.ezerg.util.DateUtil;
import com.ezerg.util.StringUtil;

public class UserManageServiceImpl implements UserManageService{
	
	private BaseDao<TBuser> baseDao;
	
	public void setBaseDao(BaseDao<TBuser> baseDao) {
		this.baseDao = baseDao;
	}
	
	@Override
	public PageUtil doListUser(PageUtil pu,TBuser user) throws Exception{
		String where = " where 1=1 ";
		String userName = user.getUsername();
		String title = user.getTitle();
		int bbstype = user.getBbstype();
		if(!StringUtil.nullOrBlank(userName)){
			where +=  " and username like '%" + userName +"%'";
		}
		if(!StringUtil.nullOrBlank(title)){
			where +=  " and title like '%" + title +"%'";
		}
		if(bbstype==1){
			where +=  " and bbstype=" + bbstype;
		}
		String querySQL = "select userid,username,useremail,lastlogin,logins,article,adddate,title,userclass,oicq,lockuser,bbstype from bbs_user"+ where +" order by adddate desc";
		pu.doList(querySQL);
		return pu;
	}
	
	@Override
	public String doAddact(TBuser obj) throws Exception {
		if(obj.getUserclass()==0){
			obj.setUserclass(1);
		}
		obj.setUserpassword(StringUtil.MD5(obj.getUserpassword()));
		obj.setAdddate(DateUtil.getDateTime());
		obj.setLastlogin(DateUtil.getDateTime());
		obj.setLogins(0);
		obj.setArticle(0);
		return baseDao.doAddObj(obj);
	}

	@Override
	public String doEditact(TBuser obj, int id) throws Exception {
		TBuser obj2 = (TBuser)Jdb.get().get(TBuser.class, id);
		obj.setUserid(id);
		if(!obj.getUserpassword().equals(obj2.getUserpassword())) {
			obj.setUserpassword(StringUtil.MD5(obj.getUserpassword()));
		} else {
			obj.setUserpassword(null);
		}
		return baseDao.doUpdateObj(TBuser.class, id, obj);
	}
	
	@Override
	public String doDelact(int id) throws Exception {
		return baseDao.doDelObj(TBuser.class, id);
	}

	@Override
	public TBuser doLoad(int id) throws Exception {
		return baseDao.doLoadObj(TBuser.class, id);
	}
	
	@Override
	public String doUpdateState(int id,String result) throws Exception {
		return baseDao.doUpdateState(TBuser.class, "userid="+id, result);
	}
}
